package {
    import flash.desktop.DockIcon;
    import flash.desktop.InteractiveIcon;
    import flash.desktop.NativeApplication;
    import flash.desktop.SystemTrayIcon;
    import flash.display.NativeMenu;
    import flash.display.NativeMenuItem;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequestDefaults;

    public class vnotify extends Sprite
    {
        [@Embed(source="../assets/favicon.png")]
        static private const FavIcon:Class;
        
        public function vnotify()  
        {
            NativeApplication.nativeApplication.autoExit = false;
            URLRequestDefaults.manageCookies = false;
            URLRequestDefaults.userAgent = "VNotify AIR Client";
            
            var menu:NativeMenu = new NativeMenu();
            var itemSettings:NativeMenuItem = new NativeMenuItem("Настройки...");
            itemSettings.addEventListener(Event.SELECT, onSettings);
            menu.addItem(itemSettings);
            menu.addItem(new NativeMenuItem(null, true));
            var itemExit:NativeMenuItem = new NativeMenuItem("Выход");
            itemExit.addEventListener(Event.SELECT, onExit);
            menu.addItem(itemExit);

            var icon:InteractiveIcon = NativeApplication.nativeApplication.icon;
            if (NativeApplication.supportsSystemTrayIcon) {
                var sysTray:SystemTrayIcon = SystemTrayIcon(icon);
                sysTray.bitmaps = [new FavIcon()]; // 16x16
                sysTray.menu = menu;
                sysTray.tooltip = "Вконтакте - Новости";
            }
            if (NativeApplication.supportsDockIcon) {
                // 128x128
                var dock:DockIcon = DockIcon(icon);
                dock.menu = menu;
            }
            stage.nativeWindow.close();
            
            var settings:Object = SettingsWindow.getSettings();
            if (settings) {
                var reactor:Reactor = new Reactor();
                reactor.login(settings.email, settings.pwd);
            }
            
        }
 
        private function onSettings(e:Event):void {
            SettingsWindow.show();
        }

        private function onExit(e:Event):void {
            NativeApplication.nativeApplication.icon.bitmaps = [];
            NativeApplication.nativeApplication.exit();
        }
    }
}
